package com.javacoo.fastdfs.pool;

import com.javacoo.fastdfs.client.internal.pool.PoolConfig;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import java.util.Scanner;

/**
 * <p>说明:</p>
 * <li></li>
 *
 * @Author DuanYong
 * @Since 2020/5/1 13:44
 * @Version 1.0
 */
@Slf4j
public class ConnectionTestFactory extends BasePooledObjectFactory<ConnectionTest> {
    @Override
    public ConnectionTest create() throws Exception {
        log.info("ConnectionFactory->create ConnectionTest");
        return new ConnectionTest();
    }

    @Override
    public PooledObject<ConnectionTest> wrap(ConnectionTest obj) {
        log.info("ConnectionFactory->wrap");
        return new DefaultPooledObject<>(obj);
    }
    @Override
    public void destroyObject(final PooledObject<ConnectionTest> p) throws Exception  {
        log.info("ConnectionFactory->destroyObject->{}",p.getObject());
        // close(p.getObject().getTrackerServer());
    }
    @Override
    public boolean validateObject(final PooledObject<ConnectionTest> p) {
        boolean ok = p.getObject().getNum().intValue() > 0;
        log.info("ConnectionFactory->validateObject->{}",ok);
        return ok;
    }

    public static void main(String[] args) {
        PoolConfig poolConfig = new PoolConfig();
        GenericObjectPoolConfig<ConnectionTest> config = new GenericObjectPoolConfig<>();
        config.setMaxTotal(3);
        config.setMaxIdle(3);
        config.setMinIdle(poolConfig.getMinIdle());
        config.setTestOnBorrow(poolConfig.isTestOnBorrow());
        config.setMaxWaitMillis(poolConfig.getMaxWaitMillis());

        GenericObjectPool<ConnectionTest> objectPool = new GenericObjectPool<>(new ConnectionTestFactory(),config);


        try {

           while(true){
               Thread.sleep(1000);
               // 获得对应key的对象
               log.info("objectPool->getNumWaiters->{},getDestroyedByBorrowValidationCount->{}",objectPool.getNumWaiters(),objectPool.getDestroyedByBorrowValidationCount());
               ConnectionTest connectionTest = objectPool.borrowObject(1000);
               log.info("borrowObject = {}->{}",connectionTest.hashCode(), connectionTest.getNum().decrementAndGet());
               objectPool.returnObject(connectionTest);
               log.info("objectPool->getNumActive->{},getNumIdle->{}",objectPool.getNumActive(),objectPool.getNumIdle());
               ConnectionTest connectionTest2 = objectPool.borrowObject(1000);
               log.info("borrowObject2 = {}", connectionTest2.getNum().decrementAndGet());
               objectPool.returnObject(connectionTest2);
               log.info("objectPool->getNumActive->{},getNumIdle->{}",objectPool.getNumActive(),objectPool.getNumIdle());
               ConnectionTest connectionTest3 = null;
               while (connectionTest3 == null) {
                   try {
                       connectionTest3 = objectPool.borrowObject(1000);

                       log.info("borrowObject3 = {}", connectionTest3.getNum().decrementAndGet());

                       objectPool.returnObject(connectionTest3);
                       break;
                   } catch (Exception e) {
                       e.printStackTrace();
                   }
               }
               //清除所有的对象
               //objectPool.clear();
           }

        } catch (Exception e) {
            log.error("", e);
        }
        new Scanner(System.in);
    }


}
